import { Vector3 } from "../../core/math/Vector3";

class LineGeometryGenerator {
    constructor(points, uvDistance = 40) {
        this.points = points.map(p => new Vector3(...p));
        this.uvDistance = uvDistance;  // UV重复的距离
        this.positions = [];
        this.directions = [];
        this.indices = [];
        this.uvs = [];
        this.vertexCount = 0;
    }

    /**
     * 计算垂直方向（平行于XY平面）
     */
    calculateNormal(p1, p2) {
        const dx = p2.x - p1.x;
        const dy = p2.y - p1.y;
        const len = Math.sqrt(dx * dx + dy * dy);
        
        return {
            x: -dy / len,
            y: dx / len
        };
    }

    /**
     * 计算线段的UV坐标
     */
    calculateSegmentUVs(p1, p2) {
        // 计算线段长度
        const dx = p2.x - p1.x;
        const dy = p2.y - p1.y;
        const segmentLength = Math.sqrt(dx * dx + dy * dy);
        
        // 计算这个线段需要重复的次数
        const repeatCount = segmentLength / this.uvDistance;
        
        return {
            start: 0,          // 起点UV始终为0
            end: repeatCount   // 终点UV为重复次数（shader会自动取模）
        };
    }

    /**
     * 生成线段顶点和方向
     */
    generateSegment(p1, p2, isFirst) {
        const normal = this.calculateNormal(p1, p2);
        const uvs = this.calculateSegmentUVs(p1, p2);
        
        if (isFirst) {
            // 起点的两个顶点
            this.positions.push(
                p1.x, p1.y, p1.z,
                p1.x, p1.y, p1.z
            );
            
            // 顶点方向
            this.directions.push(
                normal.x, normal.y, 0,
                -normal.x, -normal.y, 0
            );
            
            // 起点UV
            this.uvs.push(uvs.start, 0, uvs.start, 1);
            
            this.vertexCount += 2;
        }
        
        // 终点的两个顶点
        this.positions.push(
            p2.x, p2.y, p2.z,
            p2.x, p2.y, p2.z
        );
        
        // 终点的方向
        this.directions.push(
            normal.x, normal.y, 0,
            -normal.x, -normal.y, 0
        );
        
        // 终点UV
        this.uvs.push(uvs.end, 0, uvs.end, 1);
        
        // 添加索引
        if (isFirst) {
            this.indices.push(
                0, 2, 1,
                1, 2, 3
            );
        } else {
            const baseIndex = this.vertexCount;
            this.indices.push(
                baseIndex - 2, baseIndex, baseIndex - 1,
                baseIndex - 1, baseIndex, baseIndex + 1
            );
        }
        
        this.vertexCount += 2;
    }

    generate() {
        if (this.points.length < 2) return null;
        
        // 生成所有线段
        for (let i = 0; i < this.points.length - 1; i++) {
            this.generateSegment(this.points[i], this.points[i + 1], i === 0);
        }
        
        return {
            positions: new Float32Array(this.positions),
            directions: new Float32Array(this.directions),
            indices: new Uint32Array(this.indices),
            uvs: new Float32Array(this.uvs)
        };
    }
}

function genOneLine(message) {
    const { points, uvDistance = 40 } = message;
    const generator = new LineGeometryGenerator(points, uvDistance);
    return generator.generate();
}

export { genOneLine };